package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if (userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            } else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 分页查询我喜欢
     *
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public List<UserLike> queryMyLike(Long userId, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true))
                .limit(pagesize).skip((page - 1) * pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<UserLike> list = mongoTemplate.find(query, UserLike.class);
        return list;
    }

    /**
     * 分页查询喜欢我
     *
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public List<UserLike> queryLikeMe(Long userId, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true))
                .limit(pagesize).skip((page - 1) * pagesize).with(Sort.by(Sort.Order.desc("created")));

        return mongoTemplate.find(query, UserLike.class);

    }

    //查看喜欢人数
    @Override
    public Integer findloveCount(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("isLike").is(true));

        Long count = mongoTemplate.count(query, UserLike.class);


        return count.intValue();
    }

    //查看粉丝人数
    @Override
    public Integer findfanCount(Long likeUserId) {
        Query query = Query.query(Criteria.where("likeUserId").is(likeUserId)
                .and("isLike").is(true));
        Long count = mongoTemplate.count(query, UserLike.class);

        return count.intValue();
    }

    //查看互相喜欢
    @Override
    public Integer findeachLoveCount(Long userId) {
        //根据userid获得喜欢的likeUserIds
        Query query1 = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> youlike = mongoTemplate.find(query1, UserLike.class);
        //获得youlikeids
        List<Long> youlikeids = youlike.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());
        //将userid放入likeUserId查
        Query query2 = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        List<UserLike> likeme = mongoTemplate.find(query2, UserLike.class);
        //获得likemeids
        List<Long> likemeids = likeme.stream().map(UserLike::getUserId).collect(Collectors.toList());
        //获得交集
        List<Long> eachLoveids = youlikeids.stream().filter(likemeids::contains).collect(Collectors.toList());

        return eachLoveids.size();
    }

    //喜欢-取消
    @Override
    public void deleteLike(Long userId, Long likeUserId) {
        //判断是否存在，存在，删除
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId)
                .and("isLike").is(true));
        if (mongoTemplate.exists(query, UserLike.class)) {
            //删除
            mongoTemplate.remove(query, UserLike.class);
        }
    }

    @Override
    public List<UserLike> whoLikesMe(Long userId) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId)
                .and("isLike").is(true));
        return mongoTemplate.find(query, UserLike.class);
    }

    @Override
    public List<UserLike> MeLikeWho(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("isLike").is(true));
        return mongoTemplate.find(query, UserLike.class);
    }

}
